<!doctype html>

<title>CodeMirror: Java-like mode</title>
<meta charset="utf-8"/>
<link rel=stylesheet href="common/codemirror-5.10/doc/docs.css">

<link rel="stylesheet" href="common/codemirror-5.10/lib/codemirror.css">
<script src="common/codemirror-5.10/lib/codemirror.js"></script>
<script src="common/codemirror-5.10/addon/edit/matchbrackets.js"></script>
<link rel="stylesheet" href="common/codemirror-5.10/addon/hint/show-hint.css">
<script src="common/codemirror-5.10/addon/hint/show-hint.js"></script>
<script src="common/codemirror-5.10/mode/clike/clike.js"></script>
<script src="common/jquery-1.11.1/jquery.min.js"></script>
<style>.CodeMirror {border: 2px inset #dee;}</style>
 <h2>&nbsp;&nbsp;Java Code<h2>
<div><textarea id="java-code">
public class DynaClass  {
    public static void main(String[] args) throws Exception {
    	for (int i=0;i<10;i++){
         	System.out.println("Hello world!");
        }   
   }
}
</textarea></div>
<button onclick="execute();">Run!</button>&nbsp;&nbsp;&nbsp;&nbsp;运行结果如下：
 
<div><textarea id="consoleOut"   ></textarea></div>

<script>
      var javaEditor = CodeMirror.fromTextArea(document.getElementById("java-code"), {
        styleActiveLine: true,
        lineNumbers: true,
        matchBrackets: true,
        mode: "text/x-java",
        lineWrapping: true
      });
      
      var consoleOut = CodeMirror.fromTextArea(document.getElementById("consoleOut"), {
          lineNumbers: false,
          matchBrackets: true,
          readOnly:true,
          mode: "text/html"
        });
</script>
	 

<script> 

function execute(){
 
	  var src= javaEditor.getValue();
	  if (src==""){
		  alert("Please input some a jave class with a main method!");
		  return;
	  }
	  var mData = {"src":src}
	  $.ajax({
	         type:"POST",
	         url: "DynaRunServlet.do",
	         data:mData,
	         cache: false,
	         success: function(html){
	        	 consoleOut.setValue(html);
	          }
	  });  
}
</script>